package com.sendi.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class StringUtils extends org.apache.commons.lang.StringUtils
{
  public static String newString(String value, int length)
  {
    StringBuffer buffer = new StringBuffer();
    if (value == null) {
      return null;
    }
    for (int i = 0; i < length; ++i) {
      buffer.append(value);
    }
    return buffer.toString();
  }

  public static String newString(char ch, int length)
  {
    return newString(String.valueOf(ch), length);
  }

  public static String copyString(String str, int copyTimes)
  {
    StringBuffer buffer = new StringBuffer();
    if (str == null) {
      return null;
    }
    for (int i = 0; i < copyTimes; ++i) {
      buffer.append(str);
    }
    return buffer.toString();
  }

  public static int getBytesLength(String str)
  {
    if (str == null) {
      return -1;
    }
    return str.getBytes().length;
  }

  public static int indexOf(String str, String subStr, int startIndex, int occurrenceTimes)
  {
    int foundCount = 0;
    int index = startIndex;
    int substrLength = subStr.length();
    if (occurrenceTimes <= 0)
      return -1;
    if (str.length() - 1 < startIndex)
      return -1;
    if (subStr.equals(""))
      return 0;
    while (foundCount < occurrenceTimes) {
      index = str.indexOf(subStr, index);
      if (index == -1)
        return -1;
      ++foundCount;
      index += substrLength;
    }
    return (index - substrLength);
  }

  public static int indexOf(String str, String subStr, int occurrenceTimes)
  {
    return indexOf(str, subStr, 0, occurrenceTimes);
  }

  public static int indexOf(String str, String subStr, int fromIndex, boolean caseSensitive)
  {
    if (!(caseSensitive)) {
      return str.toLowerCase().indexOf(subStr.toLowerCase(), fromIndex);
    }
    return str.indexOf(subStr, fromIndex);
  }

  public static String replace(String str, String searchStr, String replaceStr, boolean caseSensitive)
  {
    String result = "";
    int i = 0;
    int j = 0;
    if (str == null) {
      return null;
    }
    if (str.equals("")) {
      return "";
    }
    if ((searchStr == null) || (searchStr.equals(""))) {
      return str;
    }
    if (replaceStr == null) {
      replaceStr = "";
    }
    while ((j = indexOf(str, searchStr, i, caseSensitive)) > -1) {
      result = result + str.substring(i, j) + replaceStr;
      i = j + searchStr.length();
    }
    result = result + str.substring(i, str.length());
    return result;
  }

  public static String replace(String str, String searchStr, String replaceStr)
  {
    return replace(str, searchStr, replaceStr, true);
  }

  public static String replace(String str, char searchChar, String replaceStr)
  {
    return replace(str, searchChar + "", replaceStr, true);
  }

  public static String replace(String str, int beginIndex, String replaceStr)
  {
    String result = null;
    if (str == null) {
      return null;
    }
    if (replaceStr == null) {
      replaceStr = "";
    }
    result = str.substring(0, beginIndex) + replaceStr + str.substring(beginIndex + replaceStr.length());

    return result;
  }

  public static String[] split(String originalString, int splitByteLength)
  {
    ArrayList list = new ArrayList();
    String strText = "";
    byte[] arrByte = null;
    int intStartIndex = 0;
    int intEndIndex = 0;
    int index = 0;
    int fixCount = 0;
    String[] arrReturn = null;

    if (originalString == null) {
      return new String[0];
    }
    if (originalString.equals("")) {
      return new String[0];
    }
    if (originalString.trim().equals("")) {
      return new String[] { "" };
    }
    if (splitByteLength <= 1) {
      return new String[] { originalString };
    }

    arrByte = originalString.getBytes();
    intEndIndex = 0;
    while (true)
    {
      intStartIndex = intEndIndex;
      intEndIndex = intStartIndex + splitByteLength;

      if (intStartIndex >= arrByte.length) {
        break;
      }

      if (intEndIndex > arrByte.length) {
        intEndIndex = arrByte.length;
        strText = new String(arrByte, intStartIndex, intEndIndex - intStartIndex);

        list.add(strText);
        break;
      }

      fixCount = 0;
      strText = new String(arrByte, intStartIndex, intEndIndex - intStartIndex);

      byte[] bytes = strText.getBytes();
      if (bytes.length < splitByteLength) {
        intEndIndex = intStartIndex + bytes.length;
      }
      for (index = intEndIndex - 1; (index >= intStartIndex) && 
        (arrByte[index] != bytes[(index - intStartIndex)]); --index)
      {
        ++fixCount;
      }

      if (fixCount > 0) {
        if (fixCount >= intEndIndex) {
          fixCount = 0;
          System.out.println("split length " + splitByteLength + " is too small.");
        }

        intEndIndex -= fixCount;

        strText = new String(arrByte, intStartIndex, intEndIndex - intStartIndex);
      }

      list.add(strText);
    }

    arrReturn = new String[list.size()];
    list.toArray(arrReturn);

    return arrReturn;
  }

  public static String[] split(String originalString, String delimiterString)
  {
    int index = 0;
    String[] returnArray = null;
    int length = 0;

    if ((originalString == null) || (delimiterString == null) || (originalString.equals("")))
    {
      return new String[0];
    }

    if ((originalString.equals("")) || (delimiterString.equals("")) || (originalString.length() < delimiterString.length()))
    {
      return new String[] { originalString };
    }

    String strTemp = originalString;
    while ((strTemp != null) && (!(strTemp.equals("")))) {
      index = strTemp.indexOf(delimiterString);
      if (index == -1) {
        break;
      }
      ++length;
      strTemp = strTemp.substring(index + delimiterString.length());
    }
    returnArray = new String[++length];

    for (int i = 0; i < length - 1; ++i) {
      index = originalString.indexOf(delimiterString);
      returnArray[i] = originalString.substring(0, index);
      originalString = originalString.substring(index + delimiterString.length());
    }

    returnArray[(length - 1)] = originalString;
    return returnArray;
  }

  public static String rightTrim(String str)
  {
    if (str == null) {
      return "";
    }
    int length = str.length();
    for (int i = length - 1; i >= 0; --i) {
      if (str.charAt(i) != ' ') {
        break;
      }
      --length;
    }
    return str.substring(0, length);
  }

  public static String leftTrim(String str)
  {
    if (str == null) {
      return "";
    }
    int start = 0;
    int i = 0; for (int n = str.length(); i < n; ++i) {
      if (str.charAt(i) != ' ') {
        break;
      }
      ++start;
    }
    return str.substring(start);
  }

  public static String absoluteTrim(String str)
  {
    String result = replace(str, " ", "");
    return result;
  }

  public static String lowerCase(String str, int beginIndex, int endIndex)
  {
    StringBuffer buffer = new StringBuffer();
    buffer.append(str.substring(0, beginIndex));
    buffer.append(str.substring(beginIndex, endIndex).toLowerCase());
    buffer.append(str.substring(endIndex));
    return buffer.toString();
  }

  public static String upperCase(String str, int beginIndex, int endIndex)
  {
    StringBuffer buffer = new StringBuffer();
    buffer.append(str.substring(0, beginIndex));
    buffer.append(str.substring(beginIndex, endIndex).toUpperCase());
    buffer.append(str.substring(endIndex));
    return buffer.toString();
  }

  public static String lowerCaseFirstChar(String iString)
  {
    String newString = iString.substring(0, 1).toLowerCase() + iString.substring(1);

    return newString;
  }

  public static String upperCaseFirstChar(String iString)
  {
    String newString = iString.substring(0, 1).toUpperCase() + iString.substring(1);

    return newString;
  }

  public static int timesOf(String str, String subStr)
  {
    int foundCount = 0;
    if (subStr.equals("")) {
      return 0;
    }
    int fromIndex = str.indexOf(subStr);
    while (fromIndex != -1) {
      ++foundCount;
      fromIndex = str.indexOf(subStr, fromIndex + subStr.length());
    }
    return foundCount;
  }

  public static int timesOf(String str, char ch)
  {
    int foundCount = 0;
    int fromIndex = str.indexOf(ch);
    while (fromIndex != -1) {
      ++foundCount;
      fromIndex = str.indexOf(ch, fromIndex + 1);
    }
    return foundCount;
  }

  public static Map<String, String> toMap(String str, String splitString)
  {
    Map map = Collections.synchronizedMap(new HashMap());

    String[] values = split(str, splitString);
    for (int i = 0; i < values.length; ++i) {
      String tempValue = values[i];
      int pos = tempValue.indexOf("=");
      String key = "";
      String value = "";
      if (pos > -1) {
        key = tempValue.substring(0, pos);
        value = tempValue.substring(pos + splitString.length());
      } else {
        key = tempValue;
      }
      map.put(key, value);
    }
    return map;
  }

  public static String native2ascii(String str)
  {
    char[] ca = str.toCharArray();
    StringBuffer buffer = new StringBuffer(ca.length * 6);
    for (int x = 0; x < ca.length; ++x) {
      char a = ca[x];
      if (a > 255)
        buffer.append("\\u").append(Integer.toHexString(a));
      else {
        buffer.append(a);
      }
    }
    return buffer.toString();
  }

  public static Map<String, String> sortEnglishNumberWord(Map<String, String> map)
  {
    Map resultMap = new LinkedHashMap(0);
    Map tempMap = new LinkedHashMap(0);

    Set<String> keys = map.keySet();

    int s = 2147483647;
    for (String key : keys) {
      if (key.indexOf("One") > -1)
        tempMap.put(Integer.valueOf(1), key);
      else if (key.indexOf("Two") > -1)
        tempMap.put(Integer.valueOf(2), key);
      else if (key.indexOf("Three") > -1)
        tempMap.put(Integer.valueOf(3), key);
      else if (key.indexOf("Four") > -1)
        tempMap.put(Integer.valueOf(4), key);
      else if (key.indexOf("Five") > -1)
        tempMap.put(Integer.valueOf(5), key);
      else if (key.indexOf("Six") > -1)
        tempMap.put(Integer.valueOf(6), key);
      else if (key.indexOf("Seven") > -1)
        tempMap.put(Integer.valueOf(7), key);
      else if (key.indexOf("Eight") > -1)
        tempMap.put(Integer.valueOf(8), key);
      else if (key.indexOf("Nine") > -1)
        tempMap.put(Integer.valueOf(9), key);
      else {
        tempMap.put(Integer.valueOf(s), key);
      }
      --s;
    }

    Set keyNum = tempMap.keySet();
    Object[] num_obj = keyNum.toArray();
    Integer[] nums = new Integer[num_obj.length];
    Integer tempInt = Integer.valueOf(0);
    for (int i = 0; i < num_obj.length; ++i) {
      nums[i] = new Integer(num_obj[i].toString());
    }
    for (int i = 0; i < nums.length; ++i) {
      for (int j = 0; j < nums.length - i - 1; ++j) {
        if (nums[j].intValue() > nums[(j + 1)].intValue()) {
          tempInt = nums[j];
          nums[j] = nums[(j + 1)];
          nums[(j + 1)] = tempInt;
        }
      }
    }

    for (Integer num : nums) {
      resultMap.put(tempMap.get(num), map.get(tempMap.get(num)));
    }
    return resultMap;
  }
  public boolean isEmptyStr(String src){
	  boolean result = true;
	  if(src != null && src.length() > 0){
		  result = false;
	  }
	  return result;
  }
  public boolean isNotEmptyStr(String src){
	  boolean result = false;
	  if(src != null && src.length() > 0){
		  result = true;
	  }
	  return result;
  }
}
